package com.rlz.framemm.viewmodel;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.rlz.base.base.BaseViewModel;
import com.rlz.base.base.Resource;
import com.rlz.base.util.download.DownloadHelper;
import com.rlz.base.util.download.FileInfo;
import com.rlz.framemm.bean.BannerBean;
import com.rlz.framemm.model.HomeModel;

import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import static com.rlz.base.util.download.config.InnerConstant.Inner.EXTRA_INTENT_DOWNLOAD;

/**
 * Created by RLZ
 * on 2021/4/8
 */
public class HomeViewModel extends BaseViewModel<HomeModel> {

    public static final String DOWN_ACTION = "download_helper_action";

    public static final String TONG_CHENG_URL = "http://s.ly.com/tTV79";
    public static final String TONG_CHENG_NAME = "同程旅游.apk";

    public static final String WAN_DOU_JIA_URL = "http://ucan.25pp.com/Wandoujia_web_seo_baidu_homepage.apk";
    public static final String WAN_DOU_JIA_NAME = "豌豆荚.apk";

    public static final String MEI_TUAN_URL = "http://www.meituan.com/mobile/download/meituan/android/meituan?from=new";
    public static final String MEI_TUAN_NAME = "美团.apk";

    public static final String TRAIN_12306_URL = "http://dynamic.12306.cn/otn/appDownload/androiddownload";
    public static final String TRAIN_12306_NAME = "12306.apk";
    private final MutableLiveData<FileInfo> mLiveData = new MutableLiveData<>();


    private BroadcastReceiver mReceiver;

    private void updateProgress(FileInfo fileInfo) {
        mLiveData.postValue(fileInfo);
    }

    public MutableLiveData<FileInfo> updateStatus() {
        return mLiveData;
    }


    public HomeViewModel(@NonNull Application application) {
        super(application);
    }

    public LiveData<Resource<List<BannerBean>>> getBanners() {
        return mRepository.getBanner();
    }

    public void download() {
        DownloadHelper.getInstance()
                .addTask(TRAIN_12306_URL, new File(getApplication().getExternalFilesDir("download"),
                        TRAIN_12306_NAME), DOWN_ACTION)
                .submit(getApplication());
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (null != intent) {
                    if (DOWN_ACTION.equals(intent.getAction())) {
                        FileInfo fileInfo = (FileInfo) intent.getSerializableExtra(EXTRA_INTENT_DOWNLOAD);
                        updateProgress(fileInfo);
                    }
                }
            }
        };
        getApplication().registerReceiver(mReceiver, new IntentFilter(DOWN_ACTION));
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mReceiver != null) {
            getApplication().unregisterReceiver(mReceiver);
        }
    }
}
